﻿using LevelEditor.Model.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace LevelEditor.Model.IO
{
    public class LevelIO
    {
        // Methods
        public static Level LoadLevel(string path)
        {
            Type[] knowedTypes = new Type[] { typeof(GameSprite), typeof(ActorSprite) };
            FileInfo spritesListXml = new FileInfo(path);
            Level level = new Level();
            if (spritesListXml.Exists)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Level), knowedTypes);
                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    level = serializer.Deserialize(stream) as Level;
                    level.FilePath = path;
                }
            }
            return level;
        }

        public static void SaveLevel(Level level, string path)
        {
            Type[] knowedTypes = new Type[] { typeof(GameSprite), typeof(ActorSprite) };
            XmlSerializer serializer = new XmlSerializer(level.GetType(), knowedTypes);
            using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                serializer.Serialize((Stream)stream, level);
            }
        }
    }


}
